package socket

import (
	"card/app/logic"
	"card/app/models"
	"card/pkg/jwt"
	"context"
	"github.com/pkg/errors"
	"github.com/spf13/cast"
)

func CheckLoginValid(token string) (*models.Member, error) {
	userId, err := jwt.NewJwt().LoginByTokenString(token)
	if err != nil {
		return nil, errors.WithStack(err)
	}
	userInfo, err := logic.NewMember(context.Background()).GetMemberById(cast.ToString(userId))
	if err != nil {
		return nil, errors.WithStack(err)
	}
	if userInfo.ID == 0 {
		return nil, errors.New("登录失效，请重新登录")
	}
	return userInfo, nil
}
